My name is
FearLess and I smoke weed

Group: Members Posts: 12 Member No.: 6 Joined:
5-February 05

|
Credits to xplOde
copy paste this code into a
new txt document and name it gui.cpp
| CODE |
#include "gui.h" #include
"cvar.h" #include "bind.h"
newgui gui;
// 0 nothing
clicked // 1 clicked on titlebar // 2 clicked on close
box // 3 clicked on minimize box // 4 clicked on an
item int window::handleClick() { ox = x; oy = y; cx =
gui.x; cy = gui.y; rx = cx - x; ry = cy - y; if(
isHiding ) { if( !pointRect(rx,ry,getTitleBox())
) return -1; // not in
window } else { if(
!pointRect(rx,ry,getWindowBox()) ) return -1; // not in
window }
if( pointRect(rx,ry,getCloseBox()) &&
hasClose ) { if(!canClose) {
isVisible = !isVisible; return
0; } return 2; // destroy item } if(
pointRect(rx,ry,getHideBox()) && hasHide
) { isHiding = !isHiding; return 3; // hide
window } if( pointRect(rx,ry,getTitleBox()) &&
hasTitle && hasDrag ) { isDragging =
true; return 1; // start dragging } for(int
i=0;i<elements.size();i++) if(
pointRect(rx,ry,elements[i].bounds) ) {
switch(elements[i].type) { case 0:
//PushButton elements[i].flags[0] = 1; // set to
pressed clickedElement = i;
if(elements[i].callback)
(elements[i].callback)(i); // send to outside
break; case 1: //CheckBox
elements[i].flags[0] = !elements[i].flags[0]; //
toggle clickedElement = i; // like it
matters if(elements[i].callback)
(elements[i].callback)(i); // send to outside
break; case 2: //TextArea //Don't do
anything break; case 3:
//InputField gui.clearFocus();
elements[i].flags[0] = 1; //has focus now
elements[i].flags[3] = 0; //move start pointer
gui.acceptKeys = i; clickedElement =
i; if(elements[i].callback)
(elements[i].callback)(i); // send to outside return
5; //special case break; case 4:
//ScrollBar //Do something eventually
break; default: clickedElement = -1;
// do we even need this? break;
} return 4; // ok we clicked on a
element } return 0; }
void
newgui::clearFocus() { acceptKeys = -1; for(int
j=0;j<gui.sWin.size();j++) for(int
i=0;i<gui.sWin[j].elements.size();i++)
if(gui.sWin[j].elements[i].type == eINPUT)
gui.sWin[j].elements[i].flags[0] = 0; }
void
window::updateDrag() { x = ox + (gui.x - cx); // add the
distant moved to original y = oy + (gui.y - cy); // add the
distant moved to original }
// a button creater element
newgui::makeButton( int x, int y, int w, char* label, void
(*call)(int),int r,int g,int b) { element
ret; ret.bounds.left =
x; strcpy(ret.data,label); //gEngfuncs.pfnDrawConsoleStringLen(label,&ret.bounds.right,&ret.bounds.bottom); ret.bounds.right
= x + w; ret.bounds.top = y; ret.bounds.bottom = y +
16; ret.type = eBUTTON; ret.flags[0] = 0; // we dont set any
other flags, lets hope thats okay ret.flags[1] =
r; ret.flags[2] = g; ret.flags[3] = b; ret.callback =
call; return ret; }
element newgui::makeCheck( int x,
int y, char* label, void (*call)(int),int value) { element
ret; ret.bounds.left = x; ret.bounds.right = x +
10; ret.bounds.top = y; ret.bounds.bottom = y +
10; ret.type = eCHECK; ret.flags[0] =
value; strcpy(ret.data,label); ret.callback = call; return
ret; }
// a text creater element newgui::makeText( int
x, int y, char* label,void (*call)(int), int flag, int r, int g, int
b ) { element ret; ret.bounds.left = x; ret.bounds.top =
y; ret.bounds.right = x; ret.bounds.bottom =
y; strcpy(ret.data,label); ret.type = eTEXT; ret.flags[0] =
flag; // for wrapping etc. ret.flags[1] = r; ret.flags[2] =
g; ret.flags[3] = b; ret.callback = call; return
ret; }
element newgui::makeInput( int x, int y, char*
label,void (*call)(int), int focus,int max,int num) { element
ret; ret.bounds.left =
x; strcpy(ret.data,label); //gEngfuncs.pfnDrawConsoleStringLen(label,&ret.bounds.right,&ret.bounds.bottom); ret.bounds.right
= x + (max * 10); ret.bounds.top = y; ret.bounds.bottom = y +
16; ret.type = eINPUT; ret.flags[0] = focus; ret.flags[1] =
max; ret.flags[2] = num; ret.flags[3] = max; ret.callback =
call; //if(ret.data = '.') { ret.data=0.0; } return
ret; }
element newgui::makeExternal( int x, int y
) { element ret; ret.bounds.left = x; ret.bounds.right =
x; ret.bounds.top = y; ret.bounds.bottom = y; ret.type =
eEXT; return ret; }
// a window creater void
newgui::CreateSWindow(int xx, int yy, int w, int h, const char* c,
bool f1, bool f2, bool f3, bool f4) { window
theWindow; theWindow.x = xx; theWindow.y = yy; theWindow.w
= w; theWindow.h = h; theWindow.title =
c; theWindow.hasClose = f1; theWindow.hasTitle =
f2; theWindow.hasDrag = f3; theWindow.hasHide =
f4; theWindow.isHiding = false; theWindow.isDragging =
false; theWindow.isVisible = true; theWindow.canClose = false;
// won't be
destroyed
sWin.push_back(theWindow); moveToTop(sWin.size()-1); }
//
a window creater void newgui::CreateTWindow(int x, int y, int w,
int h, const char* c, bool f1, bool f2, bool f3, bool
f4) { window theWindow; theWindow.x = x; theWindow.y =
y; theWindow.w = w; theWindow.h = h; theWindow.title =
c; theWindow.hasClose = f1; theWindow.hasTitle =
f2; theWindow.hasDrag = f3; theWindow.hasHide =
f4; theWindow.isHiding = false; theWindow.isDragging =
false; theWindow.isVisible = true; theWindow.canClose = true;
// can be
destroyed
sWin.push_back(theWindow); }
int
newgui::click() {
for(int
j=0;j<10;j++) { int i =
gui.windowOrder[j]; if(i>-1) {
gui.clickType = gui.sWin[i].handleClick(); gui.mouseState
= true; if( gui.clickType>-1 && gui.clickType!=
5) { moveToTop(i);
gui.clearFocus(); } if( gui.clickType == 0
) // click in window return i; if(
gui.clickType == 1 ) // start dragging timer return
i; if( gui.clickType == 2 ) // clicked on close
{ gui.sWin.erase(&gui.sWin[i]);
return -1; } if( gui.clickType == 3 ) //
clicked on minimize return i; if(
gui.clickType == 4 || gui.clickType == 5) // clicked on
object return i; } } return
-1; }
void
newgui::mouseUp() { if(gui.clickedWindow>=0) { gui.sWin[gui.clickedWindow].isDragging
= false; int e =
gui.sWin[gui.clickedWindow].clickedElement; if(e>-1) switch(gui.sWin[gui.clickedWindow].elements[e].type) {
case eBUTTON:
gui.sWin[gui.clickedWindow].elements[e].flags[0] =
0; break; default:
break; } gui.sWin[gui.clickedWindow].clickedElement
= -1; } gui.mouseState = false; }
void
newgui::mouseDown() { if(gui.clickedWindow =
click()) { if( gui.clickedWindow == -1
) { gui.mouseState = false; //lets just forget
about the click
gui.clearFocus(); } } gui.mouseState =
true; }
void newgui::moveToTop(int
index) { if(sWin[index].order == 0) return; //
already in front, don't do anything int realIndex =
sWin[index].order; for(int
i=realIndex;i>0;i--) { windowOrder[i] =
windowOrder[i-1]; if(windowOrder[i]>-1)
sWin[windowOrder[i]].order = i; } windowOrder[0] =
index; sWin[index].order = 0; }
bool pointRect(int
x,int y,wrect_t r) { return ( x>=r.left &&
x<=r.right && y>=r.top && y<=r.bottom
); } |
then make another txt document and
save this one as gui.h
| CODE |
#include <vector> #include
<string> #include "engine/wrect.h" using namespace
std; enum { eBUTTON,eCHECK,eTEXT,eINPUT,eSCROLL,eEXT };
//
use makeElement functions otherwise you // have to initialize all
the values in here class element { public: void
append(char c) { data[flags[3]] =
c; data[++flags[3]] = 0; }
int type; wrect_t
bounds; char data[8000]; // almost all elements use this int
flags[31]; // 31 flags? maybe void (*callback)(int
i); };
class
window { public: window() { x = 0; y =
0; w = 0; h = 0; title =
""; hasClose = false; hasTitle =
false; hasDrag = false; hasHide =
false; canClose = true; isVisible =
true; clickedElement = -1; order = 9; // don't be
fooled, MoveToTop is called } bool hasClose; bool
hasTitle; bool hasDrag; bool hasHide; bool
isHiding; bool isDragging; bool canClose; bool
isVisible; bool autoHide; int hideTime;
int x,y,w,h; //
currentOrigin int cx,cy; // clickedOrigin int rx,ry; //
relativeOrigin int ox,oy; // originalOrigin before
movement vector<element> elements; int
clickedElement; int order; string title;
wrect_t
getCloseBox() { wrect_t r;r.left = 3;r.top = 3;r.right =
13;r.bottom = 13;return r; }
wrect_t getHideBox() {
wrect_t r;r.left = w-13;r.top = 3;r.right = w-3;r.bottom = 13;return
r; }
wrect_t getTitleBox() { wrect_t r;r.left = 0;r.top =
0;r.right = w;r.bottom = 16;return r; }
wrect_t
getWindowBox() { wrect_t r;r.left = 0;r.top = 0;r.right =
w;r.bottom = h;return r; }
int handleClick();
void
updateDrag(); };
class newgui { public: newgui()
//default constructor IMPORTANT! { x = 320; // most
people use 640x480 or bigger y = 240; close =
false; clickedWindow = -1; mouseState =
false; clickType = -1; // 0 will
suffice acceptKeys = -1; for(int
i=0;i<10;i++) windowOrder[i] = -1; }
int x,y;
//mouse x,y bool close; // mouse clean up
flag vector<window> sWin; int windowOrder[10]; // the
display order for windows int clickedWindow; bool
mouseState; int acceptKeys; // also index into elements int
clickType; void mouseDown(); void mouseUp(); int
click(); void clearFocus(); void moveToTop(int index); void
CreateSWindow(int x, int y, int w, int h, const char* c =
"Untitled", bool f1 = true, bool f2 = true, bool f3 = true, bool f4
= true); void CreateTWindow(int x, int y, int w, int h, const
char* c = "Untitled", bool f1 = true, bool f2 = true, bool f3 =
true, bool f4 = true);
element makeButton( int x, int y, int
w, char* label, void (*call)(int),int r=255,int g=255,int
b=255); element makeText( int x, int y, char* label, void
(*call)(int), int flag=0, int r=255, int g=255, int b=255
); element makeCheck( int x, int y, char* label, void
(*call)(int),int value=0); element makeInput( int x, int y, char*
label,void (*call)(int), int focus=0,int max=3,int
num=0); element makeExternal( int x, int y ); };
extern
newgui gui; extern bool pointRect(int x,int y,wrect_t r); |
there u have xplOtions gui
code
but, also dont forget the includes
|